<?php

class Zhishidi_Question_Adminhtml_Zhishidi_QuestionController extends Mage_Adminhtml_Controller_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setTitle(Mage::helper('zhishidi_question')->__('Manage Question And Answer'));
        $this->renderLayout();
    }

    public function gridAction() {
        $this->loadLayout();
        $this->getResponse()->setBody($this->getLayout()->createBlock('zhishidi_question/adminhtml_question_grid')->toHtml());
    }

    public function saveAction() {
        if ($data = $this->getRequest()->getPost()) {
            try {
                $model = Mage::getModel('zhishidi_question/question')->load($this->getRequest()->getParam('id'));

                $model->setAnswer($data['answer']);
                $model->setStatus($data['status']);
                $model->setAnsweredOn(date('Y-m-d H:i:s', Mage::app()->getLocale()->storeTimeStamp()));
                             
                $model->save();
                

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('zhishidi_question')->__('Answer was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setDailydealsData(false);

                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return true;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setQuestionData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return false;
            }
        }
    }

    public function massStatusAction() {
        $questions = $this->getRequest()->getParam('mass_action_question');

        if (!is_array($questions)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_question')->__('Please select question'));
        } else {
            try {
                foreach ($questions as $question) {
                    Mage::getSingleton('zhishidi_question/question')
                            ->load($question)
                            ->setStatus($this->getRequest()->getParam('status'))->save();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                        Mage::helper('zhishidi_question')->__(
                                'Total of %d Question(s) were successfully updated', count($questions)
                        )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/');
    }

    public function newAction() {
        $this->_forward('edit');
    }

    public function massDeleteAction() {
        $questions = $this->getRequest()->getParam('mass_action_question');

        if (!is_array($questions)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_question')->__('Please select question'));
        } else {
            try {
                foreach ($questions as $question) {
                    $question = Mage::getModel('zhishidi_question/question')->load($question);
                    $question->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                        Mage::helper('zhishidi_question')->__(
                                'Total of %d Question(s) were successfully deleted', count($questions)
                        )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/');
    }

    public function deleteAction() {
        if ($this->getRequest()->getParam('id') > 0) {
            try {
                $model = Mage::getModel('zhishidi_question/question');
                $model->setId($this->getRequest()->getParam('id'))
                        ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Question was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }

    public function editAction() {




        $id = $this->getRequest()->getParam('id');
        $model = Mage::getModel('zhishidi_question/question');
        $model = $model->loadExtra($id);




        if ($model || $id == 0) {


            $data = Mage::getSingleton('adminhtml/session')->getQuestionData(true);
            if (!empty($data)) {
                $model->setData($data);
            }

            Mage::register('zhishidi_question_data', $model);
            $this->loadLayout();
            $this->_setActiveMenu('catalog/zhishidi_question');
            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
                $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
            }
            $this->getLayout()->getBlock('head')->setTitle(Mage::helper('zhishidi_question')->__('Answer Question'));
            $this->_addContent($this->getLayout()->createBlock('zhishidi_question/adminhtml_question_edit'))->_addLeft($this->getLayout()->createBlock('zhishidi_question/adminhtml_question_edit_tabs'));
            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_question')->__('Question does not exist'));
            $this->_redirect('*/*/');
        }
    }

}

